﻿using QLBH.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace QLBH.DATA
{
    public class KhoHangData
    {
        private static QLBHEntities db = new QLBHEntities();
        public string Error { set; get; }

        private string TaoMa()
        {
            string result = "";
            if (db.KHO_HANG.ToList().Count <= 0)
            {
                result = "0001";
            }
            else
            {
                int maxId = 0;

                foreach (var item in db.KHO_HANG.ToList())
                {
                    if (maxId < int.Parse(item.MA_KHO_HANG.Substring(2)))
                    {
                        maxId = int.Parse(item.MA_KHO_HANG.Substring(2));
                    }
                }
                switch (maxId.ToString().Length)
                {
                    case 1:
                        result = "000" + (maxId + 1);
                        break;


                    case 2:
                        result = "00" + (maxId + 1);
                        break;


                    case 3:
                        result = "0" + (maxId + 1);
                        break;


                    case 4:
                        result = "" + (maxId + 1);
                        break;
                    default:
                        break;
                }
            }
            return "KO" + result;
        }

        public List<KHO_HANG> TimKhoHangTheoTen(string ten_kho_hang)
        {
            return db.KHO_HANG.ToList().Where(p => p.TEN_KHO_HANG.Contains(ten_kho_hang) && p.DA_XOA == false).ToList().Select(p => new KHO_HANG()
                {
                    DA_XOA = p.DA_XOA,
                    DIA_CHI =p.DIA_CHI,
                    DIEN_THOAI = p.DIEN_THOAI,
                    EMAIL = p.EMAIL,
                    FAX = p.FAX,
                    LAN_SUA_CUOI = p.LAN_SUA_CUOI,
                    MA_KHO_HANG = p.MA_KHO_HANG,
                    MO_TA = p.MO_TA,
                    NGUOI_SUA = p.NGUOI_SUA,
                    TEN_KHO_HANG = p.TEN_KHO_HANG
                }).ToList();
        }

        public bool Them(KHOHANGPARAM KHO)
        {
            try
            {
                KHO_HANG kho_hang = new KHO_HANG();
                kho_hang.MA_KHO_HANG = KHO.MA_KHO_HANG;
                kho_hang.TEN_KHO_HANG = KHO.TEN_KHO_HANG;
                kho_hang.DIA_CHI = KHO.DIA_CHI;
                kho_hang.DIEN_THOAI = KHO.DIEN_THOAI;
                kho_hang.EMAIL = KHO.EMAIL;
                kho_hang.FAX = KHO.FAX;
                kho_hang.LAN_SUA_CUOI = DateTime.Now;
                kho_hang.NGUOI_SUA = KHO.NGUOI_SUA;
                kho_hang.MO_TA = KHO.MO_TA;
                kho_hang.DA_XOA = false;
                
                if (kho_hang.MA_KHO_HANG.Trim().Length <= 0)
                {
                    kho_hang.MA_KHO_HANG = TaoMa();
                }

                if (db.KHO_HANG.Any(P=>P.MA_KHO_HANG == kho_hang.MA_KHO_HANG))
                {
                    Error = "Mã kho hàng đã tồn tại";
                    return false;
                }

                db.KHO_HANG.Add(kho_hang);
                db.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                Error = ex.Message;
                return false;
            }
        }

        public bool CapNhat(KHOHANGPARAM KHO)
        {
            try
            {
                KHO_HANG kho_hang = new KHO_HANG();
                kho_hang.MA_KHO_HANG = KHO.MA_KHO_HANG;
                kho_hang.TEN_KHO_HANG = KHO.TEN_KHO_HANG;
                kho_hang.DIA_CHI = KHO.DIA_CHI;
                kho_hang.DIEN_THOAI = KHO.DIEN_THOAI;
                kho_hang.EMAIL = KHO.EMAIL;
                kho_hang.FAX = KHO.FAX;
                kho_hang.LAN_SUA_CUOI = KHO.LAN_SUA_CUOI;
                kho_hang.MO_TA = KHO.MO_TA;
                kho_hang.NGUOI_SUA = KHO.NGUOI_SUA;
                kho_hang.DA_XOA = false;

                KHO_HANG n = db.KHO_HANG.Where(p => p.MA_KHO_HANG == KHO.MA_KHO_HANG).FirstOrDefault();
                if (n == null)
                {
                    Error = "Kho hàng không tồn tại";
                    return false;
                }

                if (n.LAN_SUA_CUOI.ToString("ddMMyyyyhhmmss") != KHO.LAN_SUA_CUOI.ToString("ddMMyyyyhhmmss"))
                {
                    Error = "Kho hàng đã được sửa bởi " + n.NGUOI_SUA +" Vào lúc : " +n.LAN_SUA_CUOI.ToString("dd/MM/yyyy hh:mm:ss");
                    return false;
                }
                n.TEN_KHO_HANG = kho_hang.TEN_KHO_HANG;
                n.DIA_CHI = kho_hang.DIA_CHI;
                n.DIEN_THOAI = kho_hang.DIEN_THOAI;
                n.EMAIL = kho_hang.EMAIL;
                n.FAX = kho_hang.FAX;
                n.LAN_SUA_CUOI = kho_hang.LAN_SUA_CUOI;
                n.MO_TA = kho_hang.MO_TA;
                n.NGUOI_SUA = kho_hang.NGUOI_SUA;
               
                db.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                Error = ex.Message + "--" + ex.InnerException.Message;
                return false;
            }
        }

        public bool Xoa(string[] ma_kho_hang)
        {
            try
            {
                for (int i = 0; i < ma_kho_hang.Length; i++)
                {
                    string m = ma_kho_hang[i];
                    KHO_HANG n = db.KHO_HANG.Where(p => p.MA_KHO_HANG == m).FirstOrDefault();
                    if (n == null)
                    {
                        Error = "Kho hàng không tồn tại";
                        return false;
                    }
                    n.DA_XOA = true;
                    db.SaveChanges();
                }


                return true;
            }
            catch (Exception ex)
            {
                Error = ex.Message + "--" + ex.InnerException.Message;
                return false;
            }
        }

    }
}